//
//  CDRowView.m
//  CDshelf
//
//  Created by wangjun on 11-4-22.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "CDRowView.h"
#import "CDView.h"
@implementation CDRowView
@synthesize  _content;

- (id)initWithFrame:(CGRect)frame cdNumber:(int)cdnumber content:(NSString *)content
{
    self = [super initWithFrame:frame];
    if (self) {
        self._content=content;
        //添加logo 
        UIImageView *logoImage=[[UIImageView alloc] initWithFrame:CGRectMake(24, 13, 23, 24)];
        [logoImage setImage:[UIImage imageNamed:@"Lt-4line_03.png"]];
        [self addSubview:logoImage];
        [logoImage release];
        
        //添加的底部  横标记
        UIImageView *footView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 153, 1024, 7)];
        [footView setImage:[UIImage imageNamed:@"Lt--4line_10.png"]];
        [self addSubview:footView];
        [footView release];
        
        //添加字体 
         musicTags=[[UILabel alloc] initWithFrame:CGRectMake(54, 13, 200, 20)];
        [musicTags setFont:[UIFont fontWithName:@"Arial" size:24]];
        [musicTags setTextColor:[UIColor whiteColor]];
        [musicTags setBackgroundColor:[UIColor clearColor]];
        [musicTags setText:[NSString stringWithFormat:@"%@ (%d)",self._content,0]];
        [self addSubview:musicTags];
        

        [NSThread detachNewThreadSelector:@selector(addCD:) toTarget:self withObject:[NSNumber numberWithInt:cdnumber]];
        
    }
    return self;
}

//添加 cd
-(void)addCD:(NSNumber *) number
{
    //先暂停一段时间
    [NSThread sleepForTimeInterval:arc4random()%3];
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    for (int i=0; i<[number intValue]; i++) {
        if (i==([number intValue]-1)) {
            //最后一个
            [self performSelectorOnMainThread:@selector(addEndCDView:) withObject:[NSNumber numberWithInt:i] waitUntilDone:NO];
        }else{
            [self performSelectorOnMainThread:@selector(addCDView:) withObject:[NSNumber numberWithInt:i] waitUntilDone:NO];
        }
        [NSThread sleepForTimeInterval:0.09];      
    }
    [pool release];
}
-(void)addEndCDView:(NSNumber *)number
{
    [musicTags setText:[NSString stringWithFormat:@"%@ (%d)",self._content,[number intValue]+1]];
    CDView *cdView=[[CDView alloc] initWithFrame:CGRectMake((50+9*[number intValue]), 55, 8, 101) endView:YES];
    [self addSubview:cdView];
    [cdView release];
}
-(void)addCDView:(NSNumber *)number
{
    [musicTags setText:[NSString stringWithFormat:@"%@ (%d)",self._content,[number intValue]+1]];
    CDView *cdView=[[CDView alloc] initWithFrame:CGRectMake((33+9*[number intValue]), 55+arc4random()%3, 8, 101) endView:NO];
    [self addSubview:cdView];
    [cdView release];

}
- (void)dealloc
{
    self._content=nil;
    [self._content release];
    [musicTags release];
    [super dealloc];
}
@end
